package com.ruoyi.project.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.project.product.domain.ProductsFile;
import com.ruoyi.project.product.domain.ProductsShare;
import com.ruoyi.project.product.domain.ProductsShareFile;
import com.ruoyi.project.product.domain.ProductsShareItem;
import com.ruoyi.project.product.mapper.*;
import com.ruoyi.project.product.service.ProductsShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class ProductsShareServiceImpl extends ServiceImpl<ProductsShareMapper, ProductsShare> implements ProductsShareService {
   
   
   
    @Autowired
    private ProductsShareMapper productsShareMapper;

    @Resource
    private ProductsShareFileMapper productsShareFileMapper;

    @Resource
    private ProductsShareItemMapper productsShareItemMapper;

    @Resource
    private ProductsMapper productsMapper;
    @Autowired
    private ProductsFileMapper productsFileMapper;


    /**
     * 查询产品分享
     * @param shareOnlyId 产品分享主键
     * @return 产品分享
     */
    @Override

    public List<ProductsShareItem> selectProductsShareByShareId(String shareOnlyId)
    {
        ProductsShare productsShare = productsShareMapper.selectOne(new QueryWrapper<ProductsShare>().lambda().eq(ProductsShare::getShareOnlyId, shareOnlyId));
        List<ProductsShareItem> productsShareItems = productsShareItemMapper.selectList(new QueryWrapper<ProductsShareItem>().lambda().eq(ProductsShareItem::getShareId, productsShare.getShareId()));
        productsShareItems.forEach(s->{
            s.setProducts(productsMapper.selectById(s.getProductId()));
        });
        return productsShareItems;

    }



    /**
     * 查询产品分享列表
     * @param ProductsShare 产品分享
     * @return 产品分享
     */
    @Override

    public List<ProductsShare> selectProductsShareList(ProductsShare ProductsShare)

    {
        return productsShareMapper.selectProductsShareList(ProductsShare);

    }

    @Override
    public ProductsShare addProductsShare(ProductsShare productsShare) {
        //生成分享的地址
        String onlyId = UUID.randomUUID().toString();
        productsShare.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        productsShare.setCreateTime(DateUtils.getNowDate());
        productsShare.setShareOnlyId(onlyId);
        productsShare.setShareUrl("https://product.abzykj.cn/phone/filelist/"+onlyId);
//        productsShare.setShareUrl("http://localhost:5173/filelist/"+onlyId);
        productsShare.setDeptId(SecurityUtils.getDeptId());
        productsShareMapper.insert(productsShare);
        Set<Long> distinctProductIds = productsShare.getProductsFileList().stream()
                .map(ProductsFile::getProductId)
                .distinct()
                .collect(Collectors.toSet());
        //生成文件关联列表
        distinctProductIds.forEach(s->{
            ProductsShareItem productsShareItem = new ProductsShareItem();
            productsShareItem.setShareId(productsShare.getShareId());
            productsShareItem.setProductId(s);
            productsShareItem.setCreateBy(SecurityUtils.getUsername());
            productsShareItem.setCreateTime(DateUtils.getNowDate());
            productsShareItemMapper.insert(productsShareItem);
        });
        productsShare.getProductsFileList().forEach(s->{
            ProductsShareFile productsShareFile = new ProductsShareFile();
            productsShareFile.setProductId(s.getProductId());
            productsShareFile.setShareId(productsShare.getShareId());
            productsShareFile.setProductsFileId(s.getProductsFileId());
            productsShareFile.setCreateBy(SecurityUtils.getUsername());
            productsShareFile.setCreateTime(DateUtils.getNowDate());
            productsShareFileMapper.insert(productsShareFile);
        });

        return productsShare;

    }

    @Override
    public List<ProductsShareFile> getFileDetail(Long shareId, Long productId) {
        List<ProductsShareFile> productsShareFiles = productsShareFileMapper.selectList(new QueryWrapper<ProductsShareFile>().lambda().eq(ProductsShareFile::getProductId, productId).eq(ProductsShareFile::getShareId, shareId));
        productsShareFiles.forEach(s->{
            s.setProductsFile(productsFileMapper.selectById(s.getProductsFileId()));
        });
        return productsShareFiles;
    }

    /**

     * 批量删除产品分享

     *

     * @param shareIds 需要删除的产品分享主键

     * @return 结果

     */

    @Override

    public int deleteProductsShareByShareIds(Long[] shareIds)

    {

        return productsShareMapper.deleteProductsShareByShareIds(shareIds);

    }




}
